package com.mdc.mansion.core.common.sms.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.mdc.mansion.common.exception.BusinessException;
import com.mdc.mansion.common.util.I18nUtil;
import com.mdc.mansion.common.util.MD5Utils;
import com.mdc.mansion.common.util.http.HttpToolKit;
import com.mdc.mansion.core.common.sms.SendMobileService;

/**<p>com.mdc.mansion.core.common.sms.impl.SendMobileServiceImpl</p>
 * <p>Title: SendMobileServiceImpl</p>
 * <p>Copyright: Copyright ? 2010-2014</p>
 * @version 1.0
 */
@Service
public class SendMobileServiceImpl implements SendMobileService {
    private Logger LOGGER = LoggerFactory.getLogger(SendMobileServiceImpl.class);
    @Value("#{coreBaseConfig.sms_appkey}")
    private String appkey = "d3a5dfc0";
    @Value("#{coreBaseConfig.sms_secretkey}")
    private String secretkey = "a2403880";
    @Value("#{coreBaseConfig.sms_tempid}")
    private String tempid = "65";
    @Value("#{coreBaseConfig.sms_indTempid}")
    private String indTempid = "71";
    @Value("#{coreBaseConfig.sms_sendurl}")
    private String url = "https://www.nxtele.com/sms/intSms";
    
    private String chineseCode = "86";
    private String indonesiaCode = "62";
    
    /**
     * 发送短信验证码 
     * (non-Javadoc)
    * @param countryCode
    * @param phone
    * @param smsCode
    * @see com.mdc.mansion.core.common.sms.SendMobileService#sendSmsCode(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public void sendSmsCode(String countryCode, String phone, String smsCode) {
        // 判断countryCode是否为中国手机 
        if(chineseCode.equals(countryCode)){// 中国
            this.sendSmsCodeByTempid(countryCode, phone, smsCode,tempid);
        }else if(indonesiaCode.equals(countryCode)){// 印尼
        //    this.sendSmsContent(countryCode, phone, "Kode verifikasi adalah "+smsCode+", Anda menggunakan Hi Dining memerintahkan layanan pemulihan kata kunci, terbunuh jangan beritahu orang lain Oh!");
            this.sendSmsCodeByTempid(countryCode, phone, smsCode,indTempid);
        }else{// 其它 发送英文
            this.sendSmsContent(countryCode, phone, "The verification code is "+smsCode+", you are authenticating haohey ordering system, do not tell others to kill!");
        }
        
    }
    /**(non-Javadoc)
    * @param phone
    * @param content
    * {"result":"提交成功","code":"0","info":"请求成功","messageid":"74078a0090f17e "}
    * @see com.mdc.mansion.core.common.sms.SendMobileService#sendMobile(java.lang.String, java.lang.String)
    * 发送国外content短信
    */  
    private void sendSmsContent(String countryCode,String phone,String content) {
        long timestamp = System.currentTimeMillis();
        String sign = this.getSign(timestamp);
        Map<String,String> data = new HashMap<>();
        data.put("appkey", appkey);
        data.put("secretkey", secretkey);
        data.put("timestamp", timestamp+"");
        data.put("sign", sign);
        data.put("content", content);
        data.put("phone", countryCode+""+phone);
        
        try {
            String response = HttpToolKit.postSSL(url, data);
            LOGGER.info(phone + "调用短信接口返回值为" + response);
            JSONObject responseJson = JSONObject.parseObject(response);
            String code =  responseJson.getString("code");
            if(!"0".equals(code)){
                throw new BusinessException(I18nUtil.getMessage("sms.code.get.error"));// 获取验证码失败
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(I18nUtil.getMessage("sms.code.get.error"));// 获取验证码失败
        }
    }
    /**
     * 发送固定模板短信
     * @author liuxiang
     * @date create_time：2018年1月16日 下午11:48:35
     * @param countryCode
     * @param phone
     * @param smsCode
     */
    private void sendSmsCodeByTempid(String countryCode, String phone, String smsCode,String tempid) {
        long timestamp = System.currentTimeMillis();
        String sign = this.getSign(timestamp);
        Map<String,String> data = new HashMap<>();
        data.put("appkey", appkey);
        data.put("secretkey", secretkey);
        data.put("timestamp", timestamp+"");
        data.put("sign", sign);
        data.put("phone", countryCode+""+phone);
        data.put("tempid", tempid);
        data.put("params", smsCode);
        try {
            String response = HttpToolKit.postSSL(url, data);
            LOGGER.info(phone + "调用短信接口返回值为" + response);
            JSONObject responseJson = JSONObject.parseObject(response);
            String code =  responseJson.getString("code");
            if(!"0".equals(code)){
                throw new BusinessException(I18nUtil.getMessage("sms.code.get.error"));// 获取验证码失败
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(I18nUtil.getMessage("sms.code.get.error"));// 获取验证码失败
        }
    }
    /**
     * 生成sign
     */
    private String getSign(long timestamp){
        String paramString = appkey+secretkey+timestamp;
        String md5String = MD5Utils.string2MD5(paramString);
        return md5String.toUpperCase();
    }
    
    public static void main(String[] args) {
        SendMobileServiceImpl test = new SendMobileServiceImpl();
        String phone ="81388815128";
        String countryCode = "62";
        String code = "4534";
        test.sendSmsCode(countryCode, phone, code);
    }
}
